<template>
    <div class="container">

        <div class="body_wrap">
            <el-card class="body_card" shadow="hover" ref="content_card">
                <div class="header_back_wrap">
                    <el-page-header @back="$router.back(-1)" :content="$route.meta.title" class="headerBack"></el-page-header>
                    <el-divider></el-divider>
                </div>

                <div class="form_content">
                    <div class="table_form_wrap">
                        <el-form ref="form" :model="form" :rules="rules" @keyup.enter.native="handleSubmit()" label-width="120px">
                            <el-form-item label="优惠卷类型[0->全场赠券；1->会员赠券；2->购物赠券；3->注册赠券]" prop="couponType">
                                <el-input placeholder="" v-model="form.couponType" clearable style="width: 30%"></el-input>
                            </el-form-item>
                            <el-form-item label="优惠券图片" prop="couponImg">
                                <el-input placeholder="" v-model="form.couponImg" clearable style="width: 30%"></el-input>
                            </el-form-item>
                            <el-form-item label="优惠卷名字" prop="couponName">
                                <el-input placeholder="" v-model="form.couponName" clearable style="width: 30%"></el-input>
                            </el-form-item>
                            <el-form-item label="数量" prop="num">
                                <el-input placeholder="" v-model="form.num" clearable style="width: 30%"></el-input>
                            </el-form-item>
                            <el-form-item label="金额" prop="amount">
                                <el-input placeholder="" v-model="form.amount" clearable style="width: 30%"></el-input>
                            </el-form-item>
                            <el-form-item label="每人限领张数" prop="perLimit">
                                <el-input placeholder="" v-model="form.perLimit" clearable style="width: 30%"></el-input>
                            </el-form-item>
                            <el-form-item label="使用门槛" prop="minPoint">
                                <el-input placeholder="" v-model="form.minPoint" clearable style="width: 30%"></el-input>
                            </el-form-item>
                            <el-form-item label="开始时间" prop="startTime">
                                <el-input placeholder="" v-model="form.startTime" clearable style="width: 30%"></el-input>
                            </el-form-item>
                            <el-form-item label="结束时间" prop="endTime">
                                <el-input placeholder="" v-model="form.endTime" clearable style="width: 30%"></el-input>
                            </el-form-item>
                            <el-form-item label="使用类型[0->全场通用；1->指定分类；2->指定商品]" prop="useType">
                                <el-input placeholder="" v-model="form.useType" clearable style="width: 30%"></el-input>
                            </el-form-item>
                            <el-form-item label="备注" prop="note">
                                <el-input placeholder="" v-model="form.note" clearable style="width: 30%"></el-input>
                            </el-form-item>
                            <el-form-item label="发行数量" prop="publishCount">
                                <el-input placeholder="" v-model="form.publishCount" clearable style="width: 30%"></el-input>
                            </el-form-item>
                            <el-form-item label="已使用数量" prop="useCount">
                                <el-input placeholder="" v-model="form.useCount" clearable style="width: 30%"></el-input>
                            </el-form-item>
                            <el-form-item label="领取数量" prop="receiveCount">
                                <el-input placeholder="" v-model="form.receiveCount" clearable style="width: 30%"></el-input>
                            </el-form-item>
                            <el-form-item label="可以领取的开始日期" prop="enableStartTime">
                                <el-input placeholder="" v-model="form.enableStartTime" clearable style="width: 30%"></el-input>
                            </el-form-item>
                            <el-form-item label="可以领取的结束日期" prop="enableEndTime">
                                <el-input placeholder="" v-model="form.enableEndTime" clearable style="width: 30%"></el-input>
                            </el-form-item>
                            <el-form-item label="优惠码" prop="code">
                                <el-input placeholder="" v-model="form.code" clearable style="width: 30%"></el-input>
                            </el-form-item>
                            <el-form-item label="可以领取的会员等级[0->不限等级，其他-对应等级]" prop="memberLevel">
                                <el-input placeholder="" v-model="form.memberLevel" clearable style="width: 30%"></el-input>
                            </el-form-item>
                            <el-form-item label="发布状态[0-未发布，1-已发布]" prop="publish">
                                <el-input placeholder="" v-model="form.publish" clearable style="width: 30%"></el-input>
                            </el-form-item>
                            <!--
                            <el-form-item label="状态" prop="status">
                                <el-radio-group v-model="form.status">
                                    <el-radio-button :label="1">正常</el-radio-button>
                                    <el-radio-button :label="0">锁定</el-radio-button>
                                </el-radio-group>
                            </el-form-item>
                            <el-form-item label="性别" prop="sex">
                                <el-select v-model="form.sex" placeholder="请选择">
                                    <el-option label="男" value="男"></el-option>
                                    <el-option label="女" value="女"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="生日" prop="birthday">
                                <el-date-picker
                                    v-model="form.birthday"
                                    type="date"
                                    placeholder="选择日期"
                                    value-format="yyyy-MM-dd"
                                    style="width: 63%;">
                                </el-date-picker>
                            </el-form-item>
                            <el-form-item label="备注" prop="remarks">
                                <el-input v-model="form.remarks" type="textarea" :rows="4" placeholder="请输入内容" style="width: 80%;"></el-input>
                            </el-form-item>
                            -->

                        </el-form>
                    </div>
                </div>

                <div class="foot_submit">
                    <el-button @click="$router.back(-1)">返 回</el-button>
                    <el-button @click="handleSubmit" type="primary" :loading="submitLoading">提 交</el-button>
                </div>

            </el-card>
        </div>



    </div>
</template>

<script>
    export default {
        data() {
            return {
                submitLoading: false,
                form: {
                    couponType: null,
                    couponImg: null,
                    couponName: null,
                    num: null,
                    amount: null,
                    perLimit: null,
                    minPoint: null,
                    startTime: null,
                    endTime: null,
                    useType: null,
                    note: null,
                    publishCount: null,
                    useCount: null,
                    receiveCount: null,
                    enableStartTime: null,
                    enableEndTime: null,
                    code: null,
                    memberLevel: null,
                    publish: null
                },
                rules: {
                    couponType: [
                        { required: true, message: '优惠卷类型[0->全场赠券；1->会员赠券；2->购物赠券；3->注册赠券]不能为空', trigger: 'blur' }
                    ],
                    couponImg: [
                        { required: true, message: '优惠券图片不能为空', trigger: 'blur' }
                    ],
                    couponName: [
                        { required: true, message: '优惠卷名字不能为空', trigger: 'blur' }
                    ],
                    num: [
                        { required: true, message: '数量不能为空', trigger: 'blur' }
                    ],
                    amount: [
                        { required: true, message: '金额不能为空', trigger: 'blur' }
                    ],
                    perLimit: [
                        { required: true, message: '每人限领张数不能为空', trigger: 'blur' }
                    ],
                    minPoint: [
                        { required: true, message: '使用门槛不能为空', trigger: 'blur' }
                    ],
                    startTime: [
                        { required: true, message: '开始时间不能为空', trigger: 'blur' }
                    ],
                    endTime: [
                        { required: true, message: '结束时间不能为空', trigger: 'blur' }
                    ],
                    useType: [
                        { required: true, message: '使用类型[0->全场通用；1->指定分类；2->指定商品]不能为空', trigger: 'blur' }
                    ],
                    note: [
                        { required: true, message: '备注不能为空', trigger: 'blur' }
                    ],
                    publishCount: [
                        { required: true, message: '发行数量不能为空', trigger: 'blur' }
                    ],
                    useCount: [
                        { required: true, message: '已使用数量不能为空', trigger: 'blur' }
                    ],
                    receiveCount: [
                        { required: true, message: '领取数量不能为空', trigger: 'blur' }
                    ],
                    enableStartTime: [
                        { required: true, message: '可以领取的开始日期不能为空', trigger: 'blur' }
                    ],
                    enableEndTime: [
                        { required: true, message: '可以领取的结束日期不能为空', trigger: 'blur' }
                    ],
                    code: [
                        { required: true, message: '优惠码不能为空', trigger: 'blur' }
                    ],
                    memberLevel: [
                        { required: true, message: '可以领取的会员等级[0->不限等级，其他-对应等级]不能为空', trigger: 'blur' }
                    ],
                    publish: [
                        { required: true, message: '发布状态[0-未发布，1-已发布]不能为空', trigger: 'blur' }
                    ],
                }
            }
        },

        created() {
            this.form.id = this.$route.query.id
        },

        mounted() {
            this.$nextTick(() => {
                if(this.form.id) {
                    this.getInfo(this.form.id)
                }
            })
        },

        methods: {

            /**
             * 详细信息
             */
            async getInfo(id) {
                if (id) {
                    const res = await this.$http({
                        url: '/mall/umscoupon/info/' + id,
                        method: 'GET'
                    })
                    this.form = Object.assign({}, res.data)
                }
            },


            /**
             * 提交表单
             */
            async handleSubmit() {
                const refs = this.$refs
                this.submitLoading = true
                const valid = await refs['form'].validate().catch(() => {})
                if(valid) {
                    const res = await this.$http({
                        url: `/mall/umscoupon/${!this.form.id ? 'save' : 'update'}`,
                        method: 'POST',
                        data: this.form
                    })
                    if (this.ResultMessage(res)) {
                        this.$router.back(-1)
                        refs['form'].resetFields()

                        this.submitLoading = false
                    } else {
                        this.submitLoading = false
                    }
                } else {
                    this.submitLoading = false
                }
            }
        }
    }
</script>

<style lang="scss" scoped>



</style>
